<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">


<head>
    <meta charset="UTF-8" />
    <title>上传固件</title>
    <!--<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>-->
    <script src="${ctxPath}/assets/common/plugins/jquery/jquery.min.js"></script>
    <script type="text/javascript">
    </script>
    <script src="https://cdn.bootcss.com/xlsx/0.11.9/xlsx.core.min.js"></script>
</head>
<body>
<h1 th:inlines="text">文件上传</h1>
<form action="/firmware/fileUpload" method="post" enctype="multipart/form-data">
    <p>选择文件: <input type="file" name="fileName"/></p>
    <p><input type="submit" value="提交"/></p>
</form>
<p>选择excel文件: <input type="file" id="open-excel" /></p>
<p id="content"></p>

<script>

    $('#open-excel').change(function(e) {
        var files = e.target.files;
        var fileReader = new FileReader();
        fileReader.onload = function (ev) {
            try {
                var data = ev.target.result,
                    workbook = XLSX.read(data, {
                        type: 'binary'
                    }),
                    values = []; // 存储获取到的数据
                console.log('存储获取到的数据');
            } catch (e) {
                console.log('文件类型不正确');
                return;
            }

            // 遍历每张表读取
            for (var sheet in workbook.Sheets) {
                console.log('遍历每张表读取');
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    values = values.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                    //break; // 如果只取第一张表，就取消注释这行
                }
            }
            // console.log(values);
            for(var i=0;i<values.length;i++){
                console.log(values[i]);
                var str1=JSON.stringify(values[i]);
                $('#content').html($('#content').text()+"<br/>"+str1);
            }

        }
        // 以二进制方式打开文件
        fileReader.readAsBinaryString(files[0]);
    });
</script>
</body>
</html>